package com.example.adouble.cehuamenu.ui.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.adouble.cehuamenu.R;
import com.example.adouble.cehuamenu.mode.Book;

import java.util.List;
/**
 * 图书显示适配器
 */

/**
 * Created by dOUBLE on 2018/5/21.
 */

public class BookAdapter extends RecyclerView.Adapter<BookAdapter.RecyclerHolder> {

    private List<Book> list;
    private Context context;
    private ItemClickListener itemClickListener;

    public BookAdapter(Context context,List<Book> list){
        this.list = list;
        this.context = context;
    }

    /*
    *item点击事件回调接口
    */
    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }

    class RecyclerHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ItemClickListener mListener;
        private TextView tv_name;
        private TextView tv_writer;
        private TextView tv_published;
        private TextView tv_bookviews;
        public RecyclerHolder(View itemView,ItemClickListener myItemClickListener) {

            super(itemView);
            tv_name = itemView.findViewById(R.id.name);
            tv_writer = itemView.findViewById(R.id.writer);
            tv_published = itemView.findViewById(R.id.published);
            tv_bookviews = itemView.findViewById(R.id.bookviews);
            this.mListener = myItemClickListener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if(mListener != null){
                mListener.onItemClick(itemView, (Integer) view.getTag());
            }

        }
    }

    @Override
    public BookAdapter.RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        RecyclerHolder holder = new RecyclerHolder(view,itemClickListener);
        return holder;
    }


    @Override
    public void onBindViewHolder(BookAdapter.RecyclerHolder holder, int position) {

        Book book = list.get(position);
        holder.tv_name.setText(book.getB_name());
        holder.tv_bookviews.setText("浏览量："+book.getB_pageviews());
        holder.tv_writer.setText(book.getB_writer());
        holder.tv_published.setText(book.getB_published());
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setItemOnClickListener(ItemClickListener listener){
        this.itemClickListener = listener;
    }
}


